package com.feisi.dao;

import com.feisi.dao.sqlProvider.ContractSqlProvider;
import com.feisi.domain.Contract;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ContractDao {
    /**
     * 查询所有合同
     *
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from contract")
    List<Contract> findAll(@Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 查询总条数
     *
     * @return
     */
    @Select("select count(*) from contract")
    Integer contractsTotal();


    /**
     * 根据合同名称查询合同
     *
     * @param co_name
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from contract where co_name=#{co_name} limit #{page},#{limit}")
    List<Contract> findByName(@Param("co_name") String co_name, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据合同名称查询总条数
     *
     * @param co_name
     * @return
     */
    @Select("select count(*) from contract where co_name=#{co_name}")
    Integer contractsTotalByName(@Param("co_name") String co_name);

    /**
     * 根据合同状态查询
     * @param co_status
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from contract where co_status=#{co_status} limit #{page},#{limit}")
    List<Contract> findByStatus(@Param("co_status") String co_status,@Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据合同状态查询的结果数
     * @param co_status
     * @return
     */
    @Select("select count(*) from contract where co_status=#{co_status}")
    Integer contractsTotalByStatus(@Param("co_status") String co_status);


    /**
     * 新增合同
     *
     * @param contract
     */
    @Insert("insert into contract values(#{id},#{co_name},#{co_number},#{co_custname},#{co_sign_time}," +
            "#{co_money},#{co_start_time},#{co_end_time},#{co_cust_sign_name}," +
            "#{co_us_name},#{co_create_time},#{co_creator},#{co_approver},#{co_status})")
    void saveContract(Contract contract);

    /**
     * 根据id删除合同
     * 批量删除
     *
     * @param ids
     */
    @DeleteProvider(type = ContractSqlProvider.class, method = "deleteByIds")
    void deleteContract(@Param("ids") Integer[] ids);

    /**
     * 改变合同状态(已通过,未通过,待审批)
     * @param status
     */
    @Update("update contract set co_status=#{status} where id=#{id}")
    void updateStatus(@Param("id") Integer id, @Param("status") String status);

}
